package fr.sharpe.app.earthvisualizationproject.ui;

import gov.nasa.worldwind.BasicModel;
import gov.nasa.worldwind.Configuration;
import gov.nasa.worldwind.awt.WorldWindowGLCanvas;
import gov.nasa.worldwind.layers.Layer;
import gov.nasa.worldwind.layers.LayerList;
import gov.nasa.worldwind.layers.ScalebarLayer;

import java.util.HashMap;

public class Earth {

	private static WorldWindowGLCanvas wwd;
	private static Earth earth = null;
	
	private HashMap<String, Layer> layers = null;
	private HashMap<String, Layer> layersInit = null;
	
	public Earth()
	{
		if (Configuration.isMacOS())
        {
            System.setProperty("com.apple.mrj.application.apple.menu.about.name", "Hello World Wind");
        }
		
		this.setWwd(new WorldWindowGLCanvas());
		wwd.setModel(new BasicModel());
		this.layers = new HashMap<String, Layer>();
		this.layersInit = new HashMap<String, Layer>();
		DataDisplayLayer data = new DataDisplayLayer();
		data.setName("DataDisplayLayer");
		addLayer(data);
		initLayers();
//		AnnotationLayer anno = new AnnotationLayer();
//		anno.setName("Annotation");
//		MarkerLayer markers = new MarkerLayer();
//		markers.setName("Markers");
//		RenderableLayer markers2 = new RenderableLayer();
//		markers2.setName("Lines");
//		
//		SurfaceShapeLayer shapes = new SurfaceShapeLayer();
//		shapes.setName("Shapes");
//		
//		AirspaceLayer air = new AirspaceLayer();
//		air.setName("Airspace");
//		
//		addLayer(air);
//		addLayer(shapes);
//		addLayer(anno);
//		addLayer(markers2);
//		addLayer(markers);
//		displayLayers();
		
		
	}
	
	public static Earth getInstance()
	{
		if(earth == null)
		{
			earth = new Earth();
		}
		return earth;
	}
	
	public void initLayers()
	{
		LayerList layers = wwd.getModel().getLayers();
        for (Layer l : layers)
        {
            this.layers.put(l.getName(), l);
            this.layersInit.put(l.getName(), l);
        }
        displayLayers();
	}
	
	public void addLayer(Layer layer)
	{
		if(this.layers.containsKey(layer.getName()))
		{
			System.out.println("Exist");
		}
		else
		{
			System.out.println("Adding");
			this.layers.put(layer.getName(), layer);
			wwd.getModel().getLayers().add(layer);
			wwd.repaint();
		}
	}
	
	public Layer getLayer(String layerName) {
		return this.layers.get(layerName);
	}
	
	public void removeLayer(String layerName)
	{
		this.layers.remove(layerName);
		wwd.getModel().getLayers().removeKey(layerName);
		wwd.getModel().getLayers().remove(wwd.getModel().getLayers().getLayerByName(layerName));
		System.out.println("Removing layer: "+layerName);
		wwd.repaint();
	}
	
	public void displayLayers()
	{
		for(String layernName : this.layers.keySet())
		{
			System.out.println(layernName);
		}
	}

	public static void setWwd(WorldWindowGLCanvas wwd) {
		Earth.wwd = wwd;
	}

	public static WorldWindowGLCanvas getWwd() {
		return wwd;
	}

	public HashMap<String, Layer> getLayersInit() {
		return layersInit;
	}

	
}
